import React, { PureComponent } from 'react';

import { Tabs } from 'antd';
import router from 'umi/router';
import _ from 'lodash';
import * as apis from '@/services/ContractPlanMgr';

const { TabPane } = Tabs;

export default class CurrentStage extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    // console.log(this.props, 'current');
  }

  render() {
    let activeKey = '';
    const { orgStructure } = this.props;
    const { level1, level2, project, stage, category } = this.props.match.params;
    let stages = {};
    if (!_.isEmpty(orgStructure.data)) {
      stages = _.filter(orgStructure.data, {
        parentOrgid: project,
      });
    }
    stages = _.orderBy(stages, ['orgorder'], ['asc']);

    // let activeKey = '';

    if (this.props.location.pathname.search('EntireProject') === -1) {
      if (stage) {
        activeKey = stage;
      } else {
        activeKey = stages[0].orgdepid;
      }
    } else {
      activeKey = 'EntireProject';
    }

    if (!_.isEmpty(orgStructure.data)) {
      return project ? (
        <div className="header-stages">
          <Tabs
            size="large"
            activeKey={activeKey}
            animated={false}
            defaultActiveKey="1"
            onChange={key => {
              if (key === 'EntireProject') {
                router.push(
                  `/OperatingPlan/ContractPlan/EntireProject/${level1}/${level2}/${project}`,
                );
              } else {
                // 根据价格管理版本当前分期最新版本信息 跳转分期最新的版本 默认跳转  CastDecision 版本
                const params = { projectFid: key };
                apis.getNewVersionTypeUsingGET({ params }).then(data => {
                  if (data && data.data && data.data !== 'pricing') {
                    router.replace(
                      `/OperatingPlan/ContractPlan/${level1}/${level2}/${project}/${key}/${data.data}`,
                    );
                  } else {
                    router.replace(
                      `/OperatingPlan/ContractPlan/${level1}/${level2}/${project}/${key}/investment`,
                    );
                  }
                });
              }
            }}
          >
            <TabPane tab="全盘签约计划" key="EntireProject" />
            {stages && stages.length
              ? stages.map(value => <TabPane tab={value.orgname} key={value.orgdepid}></TabPane>)
              : null}
          </Tabs>
        </div>
      ) : null;
    }
    return null;
  }
}
